package com.gljx.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gljx.util.CommonExport;
import com.gljx.util.GetFid;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.*;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.DropDownMenuOptionsService;
import com.gljx.web.service.GoodstableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 物品管理Controller
 *
 * @Author: WuLianFa
 * @CreationTime: 2018/5/22 0022 上午 10:37
 */
@Controller
@RequestMapping("/")
public class GoodstableController {

    @Autowired
    GoodstableService goodstableService;//处理物品信息逻辑类

    @Autowired
    private DepartmentService departmentService;//部门等信息

    @Autowired
    private CustomLogService customLogService;//日志

    @Autowired
    private DropDownMenuOptionsService dropDownMenuOptionsService;//下拉菜单内容

    /**
     * 首页物品信息
     * Created by WuLianFa on 2018/5/22 0022 上午 11:32
     */
    @RequestMapping("/goods/look.action")
    public Object goodsAllMassageList() {
        return "/backHome/itemManagement/goodsList";
    }

    /**
     * 分页
     * Created by WuLianFa on 2018/5/23 0023 10:58
     */
    @RequestMapping("/goodsAllMassageListPaging")
    @ResponseBody
    public Object goodsAllMassageListPaging(HttpServletRequest request, String goodsName, String goodsType, String goodsGgid, int currentPage, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //组织
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        String fnameL2 = (String) department.getFnameL2();
        //分页
        PageHelper.startPage(currentPage, pageSize);
        //物品信息集合
        List<Goodstable> goodsList = goodstableService.goodsAllMassageList(goodsName, goodsType, goodsGgid, fnameL2);
        for (Goodstable goodstable : goodsList) {
            if (goodstable.getWpLastmodifier() == null) {
                goodstable.setWpLastmodifier("");
            }
            if (goodstable.getWpLastmodificationtime() == null) {
                goodstable.setWpLastmodificationtime("");
            }
        }
        PageInfo<Goodstable> pageInfo = new PageInfo<>(goodsList);
        map.put("goodsList", goodsList);
        map.put("getLastPage", pageInfo.getLastPage());
        return map;
    }

    /**
     * 跳转到增加物品界面
     * Created by WuLianFa on 2018/5/22 0022 下午 2:36
     */
    @RequestMapping("/jumpToAddGoodsPage")
    public Object jumpToAddGoodsPage(HttpServletRequest request, String addFlag) {
        request.setAttribute("addFlag", addFlag);
        return "/backHome/itemManagement/addGoods";
    }

    /**
     * 增加物品信息
     * Created by WuLianFa on 2018/5/22 0022 下午 2:54
     */
    @RequestMapping("/addGoodsMassage")
    public Object addGoodsMassage(HttpServletRequest request, Goodstable goodstable) {
        //利用UUID生成fid
        goodstable.setWpId(GetFid.UUIDIntoFid());
        //创建人信息
        User user = (User) request.getSession().getAttribute("user");
        goodstable.setWpCreator(user.getUserName());
        //创建时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String format = df.format(new Date());
        goodstable.setWpCreationtime(format);
        //组织
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        String fnameL2 = (String) department.getFnameL2();
        goodstable.setWpSchoolid(fnameL2);
        goodstableService.insertSelective(goodstable);
        return "redirect:/DrivingSys/jumpToAddGoodsPage?addFlag=1";
    }

    /**
     * 通过ID删除物品信息
     * Created by WuLianFa on 2018/5/22 0022 下午 2:50
     */
    @RequestMapping("/deleteGoodsById")
    public Object deleteGoodsById(HttpServletRequest request, String[] wpIdArray, String wpId) {
        //删除记录
        if ("".equals(wpId) || wpId == null) {
            for (int i = 0; i < wpIdArray.length; i++) {
                goodstableService.deleteByPrimaryKey(wpIdArray[i]);
            }
        } else {
            goodstableService.deleteByPrimaryKey(wpId);
        }
        return "/backHome/itemManagement/goodsList";
    }

    /**
     * 根据ID查询物品信息
     * Created by WuLianFa on 2018/5/22 0022 下午 3:03
     */
    @RequestMapping("/queryGoodsMassage")
    public Object queryGoodsMassage(HttpServletRequest request, String wpId) {
        //把查询到的数据传给页面
        Goodstable goods = goodstableService.selectByPrimaryKey(wpId);
        request.setAttribute("goods", goods);
        request.setAttribute("wpId", wpId);
        queryAllTypeMassagewpType = goods.getWpType();
        queryAllSpecificationMassagewpGgid = goods.getWpGgid();
        return "/backHome/itemManagement/updateGoods";
    }

    private String queryAllTypeMassagewpType;//修改时默认类型名称
    private String queryAllSpecificationMassagewpGgid;//修改时默认规格名称

    /**
     * 修改物品信息
     * Created by WuLianFa on 2018/5/22 0022 下午 2:57
     */
    @RequestMapping("/updateGoodsMassage")
    public Object updateGoodsMassage(HttpServletRequest request, Goodstable goodstable) {
        //修改人
        User user = (User) request.getSession().getAttribute("user");
        goodstable.setWpLastmodifier(user.getUserName());
        //修改时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String format = df.format(new Date());
        goodstable.setWpLastmodificationtime(format);
        goodstableService.updateByPrimaryKeySelective(goodstable);
        return "/backHome/itemManagement/goodsList";
    }

    /**
     * 导出EXCEL表
     * Created by WuLianFa on 2018/5/22 0022 下午 3:01
     */
    @RequestMapping("/exportGoodsMassage")
    @ResponseBody
    public Object exportGoodsMassage(HttpServletResponse response, HttpServletRequest request, String[] wpIdArray) {
        List<Object> list = new ArrayList<>();
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //组织
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        String fnameL2 = (String) department.getFnameL2();
        if (wpIdArray == null || wpIdArray.length == 0) {
            List<Goodstable> goodstables = goodstableService.goodsAllMassageList(null, null, null, fnameL2);
            for (Goodstable goodstable : goodstables) {
                list.add(goodstable.getWpId());
                list.add(goodstable.getWpName());
                list.add(goodstable.getWpType());
                list.add(goodstable.getWpGgid());
                list.add(goodstable.getWpPrice());
                list.add(goodstable.getWpCreator());
                list.add(goodstable.getWpCreationtime());
                list.add(goodstable.getWpLastmodifier());
                list.add(goodstable.getWpLastmodificationtime());
                list.add(goodstable.getWpSchoolid());
            }
        } else {
            for (int i = 0; i < wpIdArray.length; i++) {
                Goodstable goodstable = goodstableService.selectByPrimaryKey(wpIdArray[i]);
                list.add(goodstable.getWpId());
                list.add(goodstable.getWpName());
                list.add(goodstable.getWpType());
                list.add(goodstable.getWpGgid());
                list.add(goodstable.getWpPrice());
                list.add(goodstable.getWpCreator());
                list.add(goodstable.getWpCreationtime());
                list.add(goodstable.getWpLastmodifier());
                list.add(goodstable.getWpLastmodificationtime());
                list.add(goodstable.getWpSchoolid());
            }
        }
        //头部数据
        String head[] = {"物品编号", "物品名称", "物品类型", "物品规格", "物品单价", "物品创建人",
                "物品创建时间", "物品修改人", "物品修改时间", "驾校名称"};
        //长度
        Integer length[] = {5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000};
        try {
            //导出Excel
            CommonExport.importExcel(response, "物品表" + TimeFormat.getNowTime2() + ".xls", "物品信息", list, head, length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/backHome/itemManagement/goodsList";
    }

    /**
     * 查询全部类型信息
     * Created by WuLianFa on 2018/5/26 0026 10:49
     */
    @RequestMapping("/queryAllTypeMassageController")
    @ResponseBody
    public Object queryAllTypeMassageController() {
        Map<String, Object> map = new HashMap<>();
        List<Typetable> typetables = dropDownMenuOptionsService.queryAllTypeMassage();
        map.put("typetables", typetables);
        map.put("queryAllTypeMassagewpType", queryAllTypeMassagewpType);
        return map;
    }

    /**
     * 删除类型
     * Created by WuLianFa on 2018/5/24 0024 09:26
     */
    @RequestMapping("/deleteTypeMassageController")
    @ResponseBody
    public Object deleteTypeMassageController(String dellxId) {
        dropDownMenuOptionsService.deleteTypeMassageById(dellxId);
        return null;
    }

    /**
     * 增加类型
     * Created by WuLianFa on 2018/5/24 0024 09:26
     */
    @RequestMapping("/addTypeMassageController")
    @ResponseBody
    public Object addTypeMassageController(String addTypeName) {
        Typetable typetable = new Typetable();
        typetable.setLxId(GetFid.UUIDIntoFid());
        typetable.setLxName(addTypeName);
        dropDownMenuOptionsService.addTypeMassage(typetable);
        return null;
    }

    /**
     * 修改类型
     * Created by WuLianFa on 2018/5/24 0024 09:26
     */
    @RequestMapping("/updateTypeMassageController")
    @ResponseBody
    public Object updateTypeMassageController(String lxId, String lxName) {
        Typetable typetable = new Typetable();
        typetable.setLxId(lxId);
        typetable.setLxName(lxName);
        dropDownMenuOptionsService.updateTypeMassage(typetable);
        return null;
    }

    /**
     * 查询全部规格信息
     * Created by WuLianFa on 2018/5/26 0026 10:52
     */
    @RequestMapping("/queryAllSpecificationMassageController")
    @ResponseBody
    public Object queryAllSpecificationMassageController() {
        Map<String, Object> map = new HashMap<>();
        List<Specificationtable> specificationtables = dropDownMenuOptionsService.queryAllSpecificationMassage();
        map.put("specificationtables", specificationtables);
        map.put("queryAllSpecificationMassagewpGgid", queryAllSpecificationMassagewpGgid);
        return map;
    }

    /**
     * 删除规格
     * Created by WuLianFa on 2018/5/24 0024 09:26
     */
    @RequestMapping("/deleteSpecificationMassageController")
    @ResponseBody
    public Object deleteSpecificationMassageController(String delggId) {
        dropDownMenuOptionsService.deleteSpecificationMassageById(delggId);
        return null;
    }

    /**
     * 增加规格
     * Created by WuLianFa on 2018/5/24 0024 09:26
     */
    @RequestMapping("/addSpecificationMassageController")
    @ResponseBody
    public Object addSpecificationMassageController(String addSpecificationName) {
        Specificationtable specificationtable = new Specificationtable();
        specificationtable.setGgId(GetFid.UUIDIntoFid());
        specificationtable.setGgName(addSpecificationName);
        dropDownMenuOptionsService.addSpecificationMassage(specificationtable);
        return null;
    }

    /**
     * 修改规格
     * Created by WuLianFa on 2018/5/24 0024 09:26
     */
    @RequestMapping("/updateSpecificationMassageController")
    @ResponseBody
    public Object updateSpecificationMassageController(String ggId, String ggName) {
        Specificationtable specificationtable = new Specificationtable();
        specificationtable.setGgId(ggId);
        specificationtable.setGgName(ggName);
        dropDownMenuOptionsService.updateSpecificationMassage(specificationtable);
        return null;
    }
}
